package com.ibeeking.found.upms.cp.rest.controller.v1.feign;

import com.ibeeking.found.common.annotation.LoginUser;
import com.ibeeking.found.common.constants.HeaderNounConstant;
import com.ibeeking.found.common.db.annotation.IgnoreDS;
import com.ibeeking.found.common.db.annotation.Read;
import com.ibeeking.found.common.entity.*;
import com.ibeeking.found.upms.api.common.query.AccountLoginQuery;
import com.ibeeking.found.upms.api.common.query.LoginAuthQuery;
import com.ibeeking.found.upms.cp.rest.service.ICpUserService;
import com.ibeeking.found.upms.cp.rest.service.ILoginService;
import com.ibeeking.found.upms.service.common.query.LogoutAuthQuery;
import com.ibeeking.nematos.constants.enums.LogClassifyEnum;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ibeeking.nematos.log.annotation.Log;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName UserController控制器
 * @Description 全局用户管理
 * @Author ibeeking
 * @Date 2020-12-02 15:29
 **/
@RestController
@RequestMapping("/cp/user/feign/v1")
public class CpUserFeignController {

    @Resource
    private ICpUserService userServiceImpl;

    @Resource
    private ILoginService loginServiceImpl;

    /**
     * 根据登录账号查询登录用户信息
     *
     * @param query
     * @return
     */
    @IgnoreDS
    @PostMapping(value = "/queryAccountLogin")
    public LoginUserDTO queryAccountLogin(@RequestBody AccountLoginQuery query) {
        return userServiceImpl.queryAccountLogin(query);
    }

    @PostMapping(value = "/queryLoginUser/{userId}")
    public UserInfoDTO queryLoginUser(@PathVariable("userId") Long userId, HttpServletRequest request) {
        return userServiceImpl.queryLoginUser(request, userId);
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "查询登录会员")
    @Read
    @PostMapping(value = "/queryLoginCpUser")
    public LoginCpUserDTO queryLoginCpUser(@RequestBody @Validated LoginAuthQuery query, HttpServletRequest request) {
        return loginServiceImpl.queryLoginCpUser(query, request.getHeader(HeaderNounConstant.TENANT_ID));
    }

    @Log(logType = OperateLogTypeEnum.QUERY, describe = "根据OpenId查询员工信息")
    @PostMapping(value = "/queryUserByOpenId/{openId}")
    public LoginCpUserDTO queryUserByWxUserId(@PathVariable("openId") String openId, HttpServletRequest request) {
        return loginServiceImpl.queryUserByOpenId(openId, request.getHeader(HeaderNounConstant.TENANT_ID));
    }

    @Read
    @Log(logType = OperateLogTypeEnum.QUERY, describe = "通过手机号UserId查询登录员工")
    @PostMapping(value = "/queryUserByMobileOpenId")
    public LoginCpUserDTO queryUserByMobileOpenId(@RequestBody @Validated LoginAuthQuery query, HttpServletRequest request) {
        return loginServiceImpl.queryUserByMobileAndOpenId(query, request.getHeader(HeaderNounConstant.TENANT_ID));
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, logClassify = LogClassifyEnum.TRACE_LOG, describe = "员工退出登录")
    @PostMapping(value = "/logout")
    public Boolean logout(@LoginUser CurrentUser currentUser, @RequestBody @Validated LogoutAuthQuery query) {
        return loginServiceImpl.logout(currentUser.getId(), query);
    }
}
